package com.caocaog.market.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caocaog.market.annotation.IgnoreAuth;
import com.caocaog.market.entity.OrderBase;
import com.caocaog.market.entity.OrderPay;
import com.caocaog.market.pay.UnionPay;
import com.caocaog.market.pay.UnionPaySDX;
import com.caocaog.market.service.IOrderBaseService;
import com.caocaog.market.service.IOrderPayService;
import com.caocaog.market.toccpt.service.ApiForOrderService;
import com.ijpay.core.kit.WxPayKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: pay notify
 * @author: Yuki Guo
 * @date: 2021年11月01日 11:34
 */

@RestController
@RequestMapping("/api/orderSDX/notify")
@Slf4j
@Api(value = "orderNotify", tags = "支付回调")
public class OrderPayNotifySDXController {
    @Autowired
    private UnionPaySDX unionPaySDX;
    @Autowired
    private IOrderBaseService orderBaseService;
    @Autowired
    private ApiForOrderService apiForOrderService;
    @Autowired
    private IOrderPayService orderPayService;

    /**
     * 回调逻辑处理:同步结算记录
     *
     * @param payNo 订单交易id
     */
    @IgnoreAuth
    // @Transactional
    @RequestMapping("nofitytest")
    @ApiOperation(value = "回调逻辑处理:同步结算记录", notes = "回调逻辑处理:同步结算记录")
    public void logicHandle(String payNo) {
        log.info("回调验证签名成功，开始处理逻辑:同步结算记录。。。。。");
        //处理成功逻辑
        System.out.println(payNo);
        // 同步结算记录
        orderBaseService.localHandler(payNo);
        // 向曹操跑腿甩单并存甩单记录；自动甩单（会检测门店设置的发单模式，如果是自动发单就向曹操跑腿进行甩单，如果是手动发单则不会向曹操跑腿进行甩单）
        Result result = apiForOrderService.dealMarketAutomaticOrder(payNo);
        log.info("向曹操跑腿甩单并存甩单记录 isSuccess:" + result.isSuccess() + " " + result.getMessage());
        if (result.isSuccess()) {
            //发单成功 更新用户订单为2待收货
            OrderPay orderPay = orderPayService.getOne(new QueryWrapper<OrderPay>().eq("pay_serno", payNo));
            OrderBase orderBase = orderBaseService.getById(orderPay.getOrderId());
            log.info("发单成功 更新用户订单为2待收货" + orderBase.getOrderNo() + " " + orderBase.getStatus());
            orderBase.setStatus("2");
            orderBaseService.updateById(orderBase);
            orderBaseService.addOrderStatusInfo(orderBase.getId(), orderBase.getOrderNo(), "", "", "商家已接单", new Date());
        }

    }

    @RequestMapping(value = "/wx", method = {RequestMethod.GET, RequestMethod.POST})
    public String wxNofity() {
        System.out.println("微信冲冲冲");
        log.info("微信订单支付回调->>>>>>>>>");
        Result<?> result = unionPaySDX.wxNotify();
        if (result.isSuccess()) {
            //订单号
            JSONObject jsonObject = (JSONObject) result.getResult();
            String payNo = jsonObject.getString("payNo");
            this.logicHandle(payNo);
            return wxResult(true);
        }
        return wxResult(false);
    }

    @RequestMapping(value = "/wxRefund", method = {RequestMethod.GET, RequestMethod.POST})
    public void wxRefundNofity() {
        log.info("微信订单退款支付回调->>>>>>>>>");
        Result<?> result = unionPaySDX.wxRefundNotify();
        log.error("订单退款支付回调->>>>>>>>>" + result.isSuccess() + result.getMessage());
    }

    /**
     * 订单支付 支付宝回调
     *
     * @return
     */
   /* @RequestMapping(value = "/ali", method = {RequestMethod.POST, RequestMethod.GET})
    public String aliVipOrderNotify() {
        System.out.println("支付宝冲冲冲");
        log.info("支付宝支付活动订单支付回调->>>>>>>>>");
        Result<?> result = unionPay.aliNotify();
        System.out.println("这是我想看得" + result);
        if (result.isSuccess()) {
            //订单号
            JSONObject jsonObject = (JSONObject)result.getResult();
            System.out.println("这是我想看得" + jsonObject);
            String payInfoId = jsonObject.getString("payInfoId");
            this.logicHandle(payInfoId);
            return "success";
        }
        return "failure";
    }*/

    /**
     * 微信回调成功返回
     *
     * @param isSuccess 是否成功
     * @return
     */
    protected String wxResult(boolean isSuccess) {
        Map<String, String> xml = new HashMap<String, String>(2);
        xml.put("return_code", isSuccess ? "SUCCESS" : "FAIL");
        xml.put("return_msg", "OK");
        return WxPayKit.toXml(xml);
    }

}
